package com.scs.application.modules.wm.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.consts.GlobalConsts;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.base.entity.Warehouse;
import com.scs.application.modules.base.service.WarehouseService;
import com.scs.application.modules.dept.dto.ApplyAutoOnWayDTO;
import com.scs.application.modules.dept.mapper.ApplyAutoMapper;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.wm.entity.Stock;
import com.scs.application.modules.wm.entity.StockSafety;
import com.scs.application.modules.wm.service.StockSafetyService;
import com.scs.application.modules.wm.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/wm/stock_safety")
public class StockSafetyController extends CrudController<StockSafetyService, StockSafety>{
    @Autowired
    private StockService stockService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private ApplyAutoMapper applyAutoMapper;

    @Override
    public PageResult<List<StockSafety>> page() {
        PageResult<List<StockSafety>> result = super.page();
        if (result.getCount() > 0) {
            List<String> warehouseIdList = result.getData().stream().map(StockSafety::getWarehouseId).distinct().collect(Collectors.toList());
            List<String> matrIdList = result.getData().stream().map(StockSafety::getMatrId).distinct().collect(Collectors.toList());
            List<Stock> stockList = stockService.list(Wrappers.<Stock>query().in("warehouse_id",warehouseIdList).in("matr_id",matrIdList));
            Map<String,Warehouse> mapWarehouse = warehouseService.list(Wrappers.<Warehouse>query().in("id",warehouseIdList)).stream().collect(Collectors.toMap(o -> o.getId(), o -> o, (o1, o2) -> o1));
            if (stockList != null && stockList.size() > 0) {
                result.getData().forEach(item -> {
                    double skuQtyDb = stockList.stream().filter(stock -> stock.getWarehouseId().equals(item.getWarehouseId()) && stock.getMatrId().equals(item.getMatrId())).mapToDouble(Stock::getSkuQty).sum();
                    item.setSkuQtyDb(skuQtyDb);
                });
            }

            //获取在途订单信息
            Map<String,List<ApplyAutoOnWayDTO>> onWayGroup = applyAutoMapper.listApplyAutoOnWay(Wrappers.query()).stream().collect(Collectors.groupingBy(ApplyAutoOnWayDTO::getApplyAutoId));
            result.getData().forEach(item -> {
                //非库房备货 和中心库房的安全库存 走采购需求开单 否则 都走配送调拨菜单
                if ((mapWarehouse.containsKey(item.getWarehouseId()) && mapWarehouse.get(item.getWarehouseId()).getFlagCenter()) || !item.getFlagCenterPurchase().equals(1)){
                    item.setNeedType(GlobalConsts.APPLYAUTO_REFTYPE_AUTO_PURCHASE);
                }else {
                    item.setNeedType(GlobalConsts.APPLYAUTO_REFTYPE_AUTO_APPLY);
                }

                // 单品数量小于安全库存
                if (item.getSkuQtyDb() < item.getSkuQtySafety()) {
                    item.setSkuQtyNeed(item.getSkuQtyPerfect() - item.getSkuQtyDb());
                }

                // 在途数量
                if (onWayGroup != null && onWayGroup.containsKey(item.getId())) {
                    item.setSkuQtyWay(onWayGroup.get(item.getId()).stream().mapToDouble(ApplyAutoOnWayDTO::getOnWayQty).sum());
                }
            });

        }
        return result;
    }
}
